<template>
    <div>
        <!--顶部操作栏-->
           <div class="permissManagerTool">
               <el-input size="small" placeholder="请输入角色英文名" v-model="role.name">
                   <template slot="prepend">ROLE_</template>
               </el-input>
               <el-input size="small" placeholder="请输入角色中文名"
                         v-model="role.nameZh"
                         @keydown.enter.native="doAddRole"></el-input>
               <el-button type="primary" size="small" icon="el-icon-plus"
                          @click="doAddRole">添加角色</el-button>
           </div>
        <!--角色列表及可访问权限树区域-->
        <div class="permissManagerMain">
            <el-collapse v-model="activeName"  accordion @change="clickAccordion">
                <el-collapse-item :title="r.nameZh" :name="r.id" v-for="(r,index) in roles" :key="index">
                    <el-card class="box-card">
                        <div slot="header" class="clearfix">
                            <span>可访问资源</span>
                            <el-button style="float: right; padding: 3px 0;color:#ff0000;" icon="el-icon-delete" type="text" @click="deleteRole(r)"></el-button>
                        </div>
                        <!--展示可访问资源树形菜单-->
                        <div>
                            <el-tree
                                    show-checkbox
                                    node-key="id"
                                    ref="tree"
                                    :key="index"
                                    :default-checked-keys="selectedMenus"
                                    :data="allMenus" :props="defaultProps"></el-tree>
                            <div style="display: flex;justify-content: flex-end">
                                <el-button @click="cancelUpdate">取消修改</el-button>
                                <el-button type="primary" @click="doUpdate(r.id,index)">确认修改</el-button>
                            </div>
                        </div>
                    </el-card>
                </el-collapse-item>

            </el-collapse>
        </div>
    </div>
</template>

<script>
    export default {
        name: "PermissManager",
        data(){
            return{
                role:{
                    name:'',
                    nameZh:''
                },
                roles:[],//所有角色数组
                activeName:-1,//当前打开面板index
                allMenus:[],//所有菜单数组
                selectedMenus:[],//根据角色访问菜单选中数组
                defaultProps: {
                    children: 'children',
                    label: 'name'
                }
            }
        },
        mounted() {
          this.initRoles();
        },
        methods:{
            deleteRole(roleData)
            {
                /*
                 根据角色id删除角色，删除角色时需要查询该角色是否有人使用
                 有人使用时无法删除角色信息
               */
                this.$confirm('此操作将永久删除['+roleData.nameZh+']角色, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest("/sys/basic/permession/role/"+roleData.id).then(resp=>{
                        if(resp)
                        {
                            this.initRoles();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            doAddRole(){
                if(this.role.name && this.role.nameZh){
                    this.postRequest("/sys/basic/permession/addRole",this.role).then(resp=>{
                        if(resp)
                        {
                            this.role.name='';
                            this.role.nameZh='';
                            this.initRoles();
                        }
                    });
                }
                else
                {
                    this.$message.error("请输入角色名");
                }
            },
            cancelUpdate(){
                this.activeName=-1;
            },
            //更新角色访问菜单树
            doUpdate(rid,index)
            {
                let tree = this.$refs.tree[index];
                let selectedKeys = tree.getCheckedKeys(true);
                let url = '/sys/basic/permession/?rid=' + rid;
                selectedKeys.forEach(key => {
                    url += '&mids=' + key;
                })
                this.putRequest(url).then(resp => {
                    if (resp) {
                        this.activeName = -1;
                    }
                })
            },
            clickAccordion(rid){
                //单击面板展开时调用初始化树形菜单方法
                if(rid)
                 {
                    this.initAllMenus();
                    this.initSelectedMenus(rid);
                 }
            },
            initSelectedMenus(rid)
            {
                this.getRequest("/sys/basic/permession/getMenuIds/"+rid).then(resp=>{
                    if(resp)
                    {
                        this.selectedMenus=resp;
                    }
                });
            },
            initAllMenus(){
                this.getRequest("/sys/basic/permession/initAllMenus/").then(resp=>{
                    if(resp)
                    {
                        this.allMenus=resp;
                    }
                });
            },
            initRoles(){
                this.getRequest("/sys/basic/permession/").then(resp=>{
                    if(resp)
                    {
                        this.roles=resp;
                    }
                });
            }
        }
    }
</script>

<style>
    .permissManagerTool{
        display: flex;
        justify-content: flex-start;
    }
    .permissManagerTool .el-input{
        width: 300px;
        margin-right: 6px;
    }
    .permissManagerMain{
        margin-top: 10px;
        width: 700px;
    }
</style>